use std::collections::HashMap;
use serde_json::Value;
use std::fs;
use std::fs::File;
use std::io::Write;
use chrono::NaiveDateTime;
use reqwest::Response;

///
/// post 请求测试
///
#[tokio::test]
async fn test_post() {
    let base_url = String::from("https://cube-thunder-retail-service.1datatech.net");
    let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjI5Miwicm5TdHIiOiJMNkpueTdUdDZIbjFyMjZ3QnNLTXNUN0l1eFVXcTlsMCJ9.VBMvWdqSFugyBXVd1qH_4iVcuDYsuVwQzRAq0Vk5blQ";
    let mut body = HashMap::new();
    body.insert("type", 3);
    let client = reqwest::Client::new();

    let response = client.post(base_url + "/dict-table/list/module")
        .header("Authorization", token)
        .json(&body)
        .send()
        .await.unwrap();
    let echo_json: Value = response.json().await.unwrap();
    let option: Option<&Value> = echo_json.get("data");
    let data_list = option.unwrap().as_array();
    let data1 = data_list.unwrap().get(0);
    println!("{data1:#?}");
}


async fn get_batch_data(batch_id: &str) -> Response {
    let base_url = String::from("https://cube-thunder-retail-service.1datatech.net");
    let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjI5Miwicm5TdHIiOiJzWjhRNTFqTXZWOTZaN1J2RGdQdUhBRFZDbWs5Y0ptcCJ9.aKkclADybExj0LuJht_AqebNGZlqgIb_XfVsDggJqrE";
    let client = reqwest::Client::new();
    client.get(base_url + "/v2/task/result/batch/" + batch_id)
        .header("Authorization", token)
        .send()
        .await.unwrap()
}

async fn test_down(batch_id: &str) {
    let response = get_batch_data(batch_id);
    let echo_json: Value = response.await.json().await.unwrap();
    let binding = echo_json.get("data").unwrap().to_string();
    let data_arr = binding.as_bytes();
    let mut file = File::create("file/batch/batch_id_".to_owned() + &batch_id.to_string() + "ssll.json").unwrap();
    file.write_all(data_arr).unwrap();
}

#[tokio::test]
async fn test_json() {
    let json_str = fs::read_to_string("file/ssll429.json").expect("TODO: panic message");
    let json_body: Value = serde_json::from_str(&json_str).unwrap();
    let data_list = json_body.get("data").unwrap().get("taskList").unwrap().as_array();
    for x in data_list.unwrap() {
        let create_time = x.get("createTime").unwrap();
        let create_time_str = create_time.as_str().unwrap();
        let data_18 = "2024-04-29 18:00:00";
        let data_day_10 = "2024-04-29 18:30:00";
        let data_18_date = NaiveDateTime::parse_from_str(data_18, "%Y-%m-%d %H:%M:%S").unwrap();
        let parsed_date = NaiveDateTime::parse_from_str(create_time_str, "%Y-%m-%d %H:%M:%S").unwrap();
        let data_day_10_date = NaiveDateTime::parse_from_str(data_day_10, "%Y-%m-%d %H:%M:%S").unwrap();
        if parsed_date < data_day_10_date && parsed_date >= data_18_date {
            let value = x.get("batchId").unwrap();
            let batch_id = value.to_string();
            test_down(batch_id.as_str()).await;
        }
    }
}

#[test]
fn test_date() {
    let data_18 = "2024-04-29 18:30:00";
    let data_10 = "2024-04-30 10:30:00";
    let time_18 = NaiveDateTime::parse_from_str(data_18, "%Y-%m-%d %H:%M:%S").unwrap();
    let time_10 = NaiveDateTime::parse_from_str(data_10, "%Y-%m-%d %H:%M:%S").unwrap();
    if time_10 > time_18 {
        println!("{}", time_18)
    }
}